import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JPanel;

public class BoardPanel extends JPanel {
	
	private int fieldSize = 64;
	private int marginSize = 10;
	
	private int fields[][] = null;
	
	private int targetRow = -1;
	private int targetCol = -1;
	
	private Font fontNumbers; 
	private FontMetrics fontNumbersMetrics;
	
	public BoardPanel() {
		fontNumbers = new Font("Arial", Font.BOLD, 32); 
		fontNumbersMetrics = getFontMetrics(fontNumbers);
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		if (fields != null) {
			int fieldX;
			int fieldY;
			int textWidth;
			
			for (int row = 0; row < fields.length; row++) {
				for (int col = 0; col < fields[row].length; col++) {
					fieldX = marginSize + col*(fieldSize + marginSize);
					fieldY = marginSize + row*(fieldSize + marginSize);
					
					if (targetRow == row && targetCol == col) {
						g.setColor(new Color(1.0f, 0.5f, 0.0f));
					}
					else {
						g.setColor(new Color(1.0f, 0.0f, 0.0f));
					}
					g.fillRect(fieldX, fieldY, fieldSize, fieldSize);
					
					if (fields[row][col] > 0) {
						textWidth = fontNumbersMetrics.stringWidth(String.valueOf(fields[row][col]));
						g.setColor(new Color(1.0f, 1.0f, 1.0f));
						g.setFont(fontNumbers);
						g.drawString(String.valueOf(fields[row][col]), 
							fieldX + fieldSize/2 - textWidth/2, 
							fieldY + fieldSize/2 + 12);
					}
				}
			}
		}
	}
	
	public void createBoard(int numFields) {
		fields = new int[numFields][numFields];
		
		int number = 1;
		for (int row = 0; row < numFields; row++) {
			for (int col = 0; col < numFields; col++) {
				if (number == numFields*numFields) {
					number = 0;
				}
				
				fields[row][col] = number;
				number++;
			}
		}
	}
	
	public void move(int direction) {
		if (fields != null) {
			int emptyRow = -1;
			int emptyCol = -1;
			
			//Szukanie pustego pola
			for (int row = 0; row < fields.length; row++) {
				for (int col = 0; col < fields[row].length; col++) {
					if (fields[row][col] == 0) {
						emptyRow = row;
						emptyCol = col; 
					}
				}
			}
			
			//Przesuwanie
			if (emptyRow >= 0 && emptyCol >= 0) {
				switch (direction) {
				case 0: //W gore
					if (emptyRow > 0) {
						fields[emptyRow][emptyCol] = fields[emptyRow-1][emptyCol];
						fields[emptyRow-1][emptyCol] = 0;
					}
					break;
				case 1: //W prawo
					if (emptyCol < fields[emptyRow].length - 1) {
						fields[emptyRow][emptyCol] = fields[emptyRow][emptyCol+1];
						fields[emptyRow][emptyCol+1] = 0;
					}
					break;
				case 2: //W dol
					if (emptyRow < fields.length - 1) {
						fields[emptyRow][emptyCol] = fields[emptyRow+1][emptyCol];
						fields[emptyRow+1][emptyCol] = 0;
					}
					break;
				case 3: //W lewo
					if (emptyCol > 0) {
						fields[emptyRow][emptyCol] = fields[emptyRow][emptyCol-1];
						fields[emptyRow][emptyCol-1] = 0;
					}
					break;
				}
			}
		}
	}
	
	public void setField(int row, int col, int value) {
		if (row < fields.length && col < fields[row].length) {
			fields[row][col] = value;
		}
	}
	
	public void setTarget(int row, int col) {
		targetRow = row;
		targetCol = col;
	}
	
	public void setTargetDirection(int direction) {
		if (fields != null) {
			int emptyRow = -1;
			int emptyCol = -1;
			
			//Szukanie pustego pola
			for (int row = 0; row < fields.length; row++) {
				for (int col = 0; col < fields[row].length; col++) {
					if (fields[row][col] == 0) {
						emptyRow = row;
						emptyCol = col; 
					}
				}
			}
			
			targetRow = emptyRow;
			targetCol = emptyCol;
			
			//Ustalanie celu
			if (emptyRow >= 0 && emptyCol >= 0) {
				switch (direction) {
				case 0: //W gore
					targetRow--;
					break;
				case 1: //W prawo
					targetCol++;
					break;
				case 2: //W dol
					targetRow++;
					break;
				case 3: //W lewo
					targetCol--;
					break;
				}
			}
		}
	}
}
